package com.along.game.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import bsh.StringUtil;

import com.along.framework.dao.base.IBaseDao;
import com.along.game.dao.RoomDAO;
import com.along.game.dao.SysMenuDAO;
import com.along.game.model.Bigpic;
import com.along.game.model.Game_table;
import com.along.game.model.Gamescene;
import com.along.game.model.GameTable;
import com.along.game.model.OnlineUser;
import com.along.game.model.Smallpic;
import com.along.game.model.SysMenu;

/*
 * DAO实现类
 */
@Repository("roomDAO")
public class RoomDAOImpl  implements RoomDAO{
	
	@Resource(name="baseDao")
	private IBaseDao<Gamescene,Long> dao;


	/**
	 * 得到所有的游戏场
	 */
	public List getSceneInfo() {
		String sql = "select * from GameScene";
		List list = dao.jdbc().list(sql, Gamescene.class);
	    return list;
	}
	
	/**
	 * 得到所有的游戏场(级别为2)
	 */
	public List getSceneInfo2() {
		String sql = "select * from GameScene where GREADE=2";
		List list = dao.jdbc().list(sql, Gamescene.class);
	    return list;
	}
	
	/**
	 * 通过级别得到所有的游戏房间(级别为3)
	 */
	public List getSceneInfoByGreade(String greade) {
		String sql = "select * from GameScene where GREADE="+greade;
		List list = dao.jdbc().list(sql, Gamescene.class);
	    return list;
	}
	
	/**
	 * 通过父节点得到所有的下面的游戏房子
	 */
	public List getSceneInfoBydeskId(String parentId) {
		String sql = "select * from GameScene where parent_Fieldid="+parentId;
		List list = dao.jdbc().list(sql, Gamescene.class);
	    return list;
	}



	/**
	 * 得到所有的房间桌
	 */
	public List getAllRoom() {
		String sql = "select * from gametable";
		List list = dao.jdbc().list(sql, Game_table.class);
	    return list;
	}


	/**
	 * 通过游戏场得到所有的房间
	 */
	public List getAllRoom(String sceneID) {
		String sql = "select * from gametable t where t.filed_id=?";
		List list = dao.jdbc().select(sql,GameTable.class, sceneID);
	    return list;
	}


	/**
	 * 通过游戏场id，得到所有的用户信息（名称，级别，和积分）
	 * @param sceneId
	 * @return
	 */
	public List getPlayerBySceneId(String sceneId) {
		String sql = "select * from online_user t where t.field_id=?";
		List list = dao.jdbc().select(sql,OnlineUser.class, sceneId);
	    return list;
	}
	
	/**
	 * 通过房间id得到房子的在线人数
	 * @param sceneId
	 * @return
	 */
	public  int getUserCountByRoomId(String sceneId) {
		String sql = "select count(*) from online_user t where t.field_id=?";
		Object[] o = {sceneId};
		return dao.jdbc().queryForInt(sql, o);
	}
	
	/**
	 * 通过游戏桌id，得到所有的户信息（名称，级别，和积分）
	 * @param sceneId
	 * @return
	 */
	public List getPlayerByDeskId(String desk_id) {
		String sql = "select * from online_user t where t.desk_id=?";
		List list = dao.jdbc().select(sql,OnlineUser.class, desk_id);
	    return list;
	}
	
	/**
	 * 通过游戏桌id，得到所有的户信息（退出游戏窗口）
	 * @param sceneId
	 * @return
	 */
	public List getPlayerByDeskId(String desk_id,String ifEnd) {
		String sql = "select * from online_user t where USER_STATE=? and t.desk_id=?";
		List list = dao.jdbc().select(sql,OnlineUser.class, ifEnd,desk_id);
	    return list;
	}
	
	/**
	 * 通过游戏桌id和位置，判断是否入座
	 * @param sceneId
	 * @return
	 */
	public boolean ifHavaPositionByDeskId(String desk_id,String position) {
		String sql = "select * from online_user t where t.desk_id=? and DESK_LOCATION=?";
		List list = dao.jdbc().select(sql,OnlineUser.class, desk_id,position);
		if(list!=null&&list.size()>0)
		{
			return true;
		}
	    return false;
	}
	
	/**
	 * 通过用户bsn得到用户状态信息
	 */
	public OnlineUser getOnlineInfoByUsn(String usn) {
		String sql = "select * from online_user t where t.usn=?";
		 Object[] object = {usn};
		 OnlineUser gameScene = (OnlineUser) dao.jdbc().queryForObject(sql,object,OnlineUser.class);
		return gameScene;
	}
	
	
	/**
	 * 通过房间id得到下面的游戏桌
	 */
	public List getDeskByRoomId(String roomId) {
		String sql = "select * from gametable t where t.filed_id=?";
		List list = dao.jdbc().select(sql, GameTable.class,roomId);
	    return list;
	}


	/**
	 * 通过房间id得到房间信息
	 */
	public Gamescene getRoomInfoById(String sceneId) {
		String sql = "select * from gamescene t where t.filed_id=?";
		 Object[] object = {sceneId};
		Gamescene gameScene = (Gamescene) dao.jdbc().queryForObject(sql,object,Gamescene.class);
		return gameScene;
	}
	
	/**
	 * 通过房子id得到房子信息
	 */
	public GameTable getDeskInfoById(String deskId) {
		String sql = "select * from  GAMETABLE t where t.DESK_ID=?";
		 Object[] object = {deskId};
		 GameTable gameTable = (GameTable) dao.jdbc().queryForObject(sql,object,GameTable.class);
		return gameTable;
	}
	
	/**
	 * 通过图片id得到大图片信息
	 * @param desk_id
	 * @return
	 */
	public Bigpic getBigPicInfoById(String bigpicid)
	{
		Bigpic bigpic = new Bigpic();
		String sql = "select * from bigpic t where t.id=?";
		 Object[] object = {bigpicid};
		Bigpic gameScene = (Bigpic) dao.jdbc().queryForObject(sql,object,Bigpic.class);
		return bigpic;
	}
	
	
	/**
	 * 通过游戏桌和大图片得到正在玩的小图片
	 * @param desk_id
	 * @return
	 */
	public List getSmallPicInfoById(String desk_id,String bigpicId)
	{
		String sql = "select * from smallPic  g where g.id in (select t.smallpic_ids from small_stateinfo t where t.desk_id=? and t.bigpic_id=?)";
		Object[] object = {desk_id,bigpicId};
		List list  = dao.jdbc().select(sql, Smallpic.class, desk_id,bigpicId);
		return list;
	}
	
	
	/**
	 * 通过游戏桌和大图片得到正在玩的小图片
	 * @param desk_id
	 * @return
	 */
	public List getSmallPicInfoById(String smallpicIds)
	{
		String sql = "select * from smallPic  g where g.id in (?)";
		Object[] object = {smallpicIds};
		List list  = dao.jdbc().select(sql, Smallpic.class, object);
		return list;
	}
	
	/**
	 * 通过游戏桌得到所有正在玩的所有小图片
	 * @param desk_id
	 * @return
	 */
//	public List getSmallPicInfoById(String desk_id)
//	{
//		String
//		String sql = "select * from smallPic  g where g.id in (select t.smallpic_ids from small_stateinfo t where t.desk_id=? and t.bigpic_id=?)";
//		Object[] object = {desk_id};
//		List list  = dao.jdbc().select(sql, Smallpic.class, desk_id);
//		return list;
//	}
	
	
	/**
	 * 通过游戏桌得到所有的正在玩的大图片,以数组返回
	 * @param desk_id
	 * @return
	 */
	public String[] getBigPicByDeskId(String desk_id)
	{
		String sql = "select * from init_deskinfo  t where t.desk_id=?";
		Object[] object = {desk_id};
		String picOrders  = dao.jdbc().queryForString(sql, object,"picid_order");
	    String str[] = StringUtil.split(picOrders, ",");
		return str;
	}


}
